
<?php 

include("../includes/init.php");

$admin_id=isset($_GET["admin_id"])?$_GET["admin_id"]:"0";

$sql="select * from {$pre_}admin where admin_id=$admin_id";
$admin=getOne($sql);

// 得到权限的数组
$current_action=explode(",", $admin["admin_action"]);

$sql="select * from {$pre_}admin_action";

$admin_action=getAll($sql);

$action_list=array();



foreach ($admin_action as $item) {
    
     if(in_array($item["action_name_en"],$admin_action)){

         $item["checked"]=true;
     }else{
     	  $item["checked"]=false;
     }



    // 分离顶级数组
	if($item["parent_id"]==0){

         $action_list[]=$item;  
	}

	foreach ($action_list as $key => $value) {
		
         // 分离子级数组
         if($value["action_id"]==$item["parent_id"]){
            $action_list[$key]["son"][]=$item;
         } 
	}


}





if($_POST){
   
   // 接收到一个数组
   $admin_action=isset($_POST["admin_action"])?$_POST["admin_action"]:"";
   
   if (is_array($admin_action)&&!empty($admin_action)) {
   	 // 分割数组为一个字符串
   $admin_action_str=implode(",",$admin_action);
   $data=array("admin_action"=>$admin_action_str);


   $affect_id=update("{$pre_}admin",$data,"admin_id=$admin_id");

   if($affect_id){

      if($admin_id == $_SESSION['admin_id']){

        //编辑自己的时候要更新缓存
        $_SESSION['admin_action'] = $admin_action_str;
      }
      show_msg('编辑权限成功',"admin_list.php");
      exit;


    }else{
      show_msg('编辑权限失败', "admin_action.php?admin_id=$admin_id");
      exit;
    }
   

     
   }


}

?>

<!doctype html>
<html class="no-js">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Amaze后台管理系统模板HTML表单页面 - 源码之家</title>
  <meta name="description" content="这是一个form页面">
  <meta name="keywords" content="form">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
  <meta name="renderer" content="webkit">
  <meta http-equiv="Cache-Control" content="no-siteapp" />
  <link rel="icon" type="image/png" href="assets/i/favicon.png">
  <link rel="apple-touch-icon-precomposed" href="assets/i/app-icon72x72@2x.png">
  <meta name="apple-mobile-web-app-title" content="Amaze UI" />
  <link rel="stylesheet" href="assets/css/amazeui.min.css"/>
  <link rel="stylesheet" href="assets/css/admin.css">
</head>
<body>
<!--[if lte IE 9]>
<p class="browsehappy">你正在使用<strong>过时</strong>的浏览器，Amaze UI 暂不支持。 请 <a href="http://browsehappy.com/" target="_blank">升级浏览器</a>
  以获得更好的体验！</p>
<![endif]-->

<?php 
include("header.php");
?>

<div class="am-cf admin-main">
<!-- sidebar start -->
<?php 
include("menu.php");
?>
<!-- sidebar end -->

<!-- content start -->
<div class="admin-content">
  <div class="am-cf am-padding">
    <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">权限</strong> / <small>form</small></div>
  </div>

  <hr/>

  <form method="post" enctype="multipart/form-data" class="am-form am-form-horizontal">
  <div class="am-tabs am-margin" data-am-tabs>
  
      <div class="am-tab-panel am-fade am-in am-active" id="tab1">
       <?php 
           foreach ($action_list as $item) {       
         ?>

        <div class="am-g am-margin-top">
          <div class="am-u-sm-2 am-text-right"><?php echo $item["action_name_zh"];?></div>

           <?php
               if( isset($item["son"])&&is_array($item["son"]) ){
            ?>
          <div class="am-u-sm-10">
            <div class="am-btn-group" data-am-button>
                 <?php 
                   foreach ($item["son"] as $value) {
                 ?>
              <label class="am-btn am-btn-default am-btn-xs <?php echo $value['checked']?"am-active":"";?>  " >
                 <!-- 选中 提交 才会被接收到 -->
                <input type="checkbox" <?php echo $value['checked']?"checked":""?> id="option1" name="admin_action[]" value="<?php echo $value["action_name_en"];?>"> 
                <?php echo $value["action_name_zh"];?>

              </label>
                <?php
                   }
                ?>
            </div>
          </div>

          <?php 
             }
          ?>
        </div>
         <?php 
           }

         ?>
      </div>

     

      

 
  </div>

 <div class="am-form-group">
            <div class="am-u-sm-9 am-u-sm-push-3">
              <button type="submit" class="am-btn am-btn-primary">权限设置</button>
            </div>
 </div>
<!-- content end -->

</form>


</div>


<?php

include("footer.php");
?>

<!--[if lt IE 9]>
<script src="assets/js/jquery1.11.1.min.js"></script>
<script src="assets/js/modernizr.js"></script>
<script src="assets/js/polyfill/rem.min.js"></script>
<script src="assets/js/polyfill/respond.min.js"></script>
<script src="assets/js/amazeui.legacy.js"></script>
<![endif]-->

<!--[if (gte IE 9)|!(IE)]><!-->
<script src="assets/js/jquery.min.js"></script>
<script src="assets/js/amazeui.min.js"></script>
<!--<![endif]-->
<script src="assets/js/app.js"></script>
</body>
</html>
